/*
 *所有关于tb_project_tsinfo类的业务代码应在此处编写
*可使用repository.调用常用方法，获取EF/Dapper等信息
*如果需要事务请使用repository.DbContextBeginTransaction
*也可使用DBServerProvider.手动获取数据库相关信息
*用户信息、权限、角色等使用UserContext.Current操作
*tb_project_tsinfoService对增、删、改查、导入、导出、审核业务代码扩展参照ServiceFunFilter
*/
using MYData.Core.BaseProvider;
using MYData.Core.Extensions.AutofacManager;
using MYData.Entity.DomainModels;
using System.Linq;
using MYData.Core.Utilities;
using System.Linq.Expressions;
using MYData.Core.Extensions;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.AspNetCore.Http;
using MYData.System.IRepositories;
using MYData.Core.EFDbContext;
using System.Threading.Tasks;
using MYData.Entity.DTO.ApiEntity.Input;
using Mapster;

namespace MYData.System.Services
{
    public partial class tb_project_tsinfoService
    {
        private readonly IHttpContextAccessor _httpContextAccessor;
        private readonly Itb_project_tsinfoRepository _repository;//访问数据库
        private readonly VOLContext _context;

        [ActivatorUtilitiesConstructor]
        public tb_project_tsinfoService(
            Itb_project_tsinfoRepository dbRepository,
            IHttpContextAccessor httpContextAccessor
,
            VOLContext context)
        : base(dbRepository)
        {
            _httpContextAccessor = httpContextAccessor;
            _repository = dbRepository;
            _context = context;
            //多租户会用到这init代码，其他情况可以不用
            //base.Init(dbRepository);
        }

        WebResponseContent webResponse = new WebResponseContent();

        /// <summary>
        /// 暂存信息表添加
        /// </summary>
        /// <param name="inputDto"></param>
        /// <returns></returns>
        public async Task<WebResponseContent> InsertTsInfoAsync(InsertBaseInfoTs_Dto inputDto)
        {
            var model = inputDto.Adapt<tb_project_tsinfo>();
            await _context.Set<tb_project_tsinfo>().AddAsync(model);
            return webResponse.OK("添加成功", await _context.SaveChangesAsync());
        }
        /// <summary>
        /// 根据用户名获取该用户暂存信息
        /// </summary>
        /// <param name="TsSearch"></param>
        /// <returns></returns>
        public async Task<WebResponseContent> TsInforSearch(TsInforSearchDto TsSearch)
        {
            var list = _context.Set<tb_project_tsinfo>().Where(x => x.userId.Equals(TsSearch.userName));
            if (!string.IsNullOrEmpty(TsSearch.projectName))
            {
                list = list.Where(x => x.projectName.Contains(TsSearch.projectName));
            }
            if (TsSearch.BeginDate != null)
            {
                list = list.Where(x => x.createTime > TsSearch.BeginDate);
            }
            if (TsSearch.EndDate != null)
            {
                list = list.Where(x => x.createTime < TsSearch.EndDate);
            }
            if (TsSearch.transientType != null)
            {
                list = list.Where(x => x.transientType.Equals(TsSearch.transientType));
            }
            object res = new { list = list.Skip((TsSearch.index - 1)).Take(TsSearch.size).ToList(), Total = list.Count() };
            return webResponse.OK("暂存信息获取成功", res);
        }

        /// <summary>
        /// 根据暂存信息的ID查询暂存信息
        /// </summary>
        /// <param name="Id">暂存信息主键</param>
        /// <returns></returns>
        public tb_project_tsinfo GetTsBaseInfoById(string Id)
        {
            return _context.Set<tb_project_tsinfo>().Find(Id);
        }
        /// <summary>
        /// 立项暂存信息删除
        /// </summary>
        /// <param name="projectid"></param>
        /// <returns></returns>
        public WebResponseContent DelTs(string id)
        {
            var ids = id.Split(',');
            foreach (var item in ids)
            {
                var model = _context.Set<tb_project_tsinfo>().Find(item);
                _context.Remove(model);
            }
            if (_context.SaveChanges() > 0)
            {
                return webResponse.OK("删除成功");
            }
            else
            {
                return webResponse.OK("删除失败");
            }
        }
    }
}
